package com.badibadi.adapter;

import java.util.List;

import net.tsz.afinal.FinalBitmap;

import com.badibadi.activity.BaseActivity;
import com.badibadi.infos.PhotoModel;
import com.badibadi.mytools.Constants;
import com.badibadi.mytools.Utils;
import com.badibadi.uniclubber.R;
import com.libjph.util.StringUtil;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

import android.app.Fragment;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

/**
 * 已改 iv_0
 * 
 * @author Administrator
 * 
 */
public class MyPolyAdapter extends BaseAdapter {

	// private int ModelFlags = 0;// 版型标记，默认为0
	Context context;
	private DisplayImageOptions options;

	private List<PhotoModel> mList;
	// 版型 有三种 默认0 1参考 ， 2四个格子
	private String Version;

	private FinalBitmap finalBitmap;

	public MyPolyAdapter(Context ct, List<PhotoModel> list, String Version) {
		context = ct;
		mList = list;
		this.Version = Version;

		options = new DisplayImageOptions.Builder()
				.showStubImage(R.drawable.ic_stub)
				.showImageForEmptyUri(R.drawable.noimage)
				.showImageOnFail(R.drawable.noimage).cacheInMemory()
				.cacheOnDisc().displayer(new RoundedBitmapDisplayer(0)).build();
		finalBitmap = FinalBitmap.create(context);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mList.get(position).getShow_img();
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub

		return position;
	}

	@Override
	public int getItemViewType(int position) {
		// TODO Auto-generated method stub
		if (Version.equals("0")) {
			return 0;
		} else if (Version.equals("1"))
			return 1;
		else if (Version.equals("2"))
			return 2;
		return 0;
	}

	@Override
	public int getViewTypeCount() {
		// TODO Auto-generated method stub
		return 3;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		LayoutInflater inflater = LayoutInflater.from(context);
//		   
		ViewHolder holder = new ViewHolder();

		if (convertView == null) {

			// 版型1
			if (getItemViewType(position) == 0) {
				convertView = inflater.inflate(
						R.layout.mypoly_adapter_import_layout, null);
				// 相册名
				holder.photo_name = (TextView) convertView
						.findViewById(R.id.photo_name);
				// img_count数量
				holder.img_count = (TextView) convertView
						.findViewById(R.id.img_count);
				holder.lock = (ImageView) convertView.findViewById(R.id.lock);

				try {
					// 大图
					holder.iv_0 = (ImageView) convertView
							.findViewById(R.id.iv_0);
					holder.iv_1 = (ImageView) convertView
							.findViewById(R.id.iv_1);
					holder.iv_2 = (ImageView) convertView
							.findViewById(R.id.iv_2);
					holder.iv_3 = (ImageView) convertView
							.findViewById(R.id.iv_3);
					holder.iv_4 = (ImageView) convertView
							.findViewById(R.id.iv_4);
				} catch (Exception e) {
					// TODO: handle exception
				}
			} // 版型2
			else if (getItemViewType(position) == 1) {
				convertView = inflater.inflate(
						R.layout.mypoly_adapter_import_layout1, null);
				// 相册名
				holder.photo_name = (TextView) convertView
						.findViewById(R.id.photo_name);
				// img_count数量
				holder.img_count = (TextView) convertView
						.findViewById(R.id.img_count);
				holder.lock = (ImageView) convertView.findViewById(R.id.lock);

				try {
					// 大图
					holder.iv_0 = (ImageView) convertView
							.findViewById(R.id.iv_0);
					holder.iv_1 = (ImageView) convertView
							.findViewById(R.id.iv_1);
					holder.iv_2 = (ImageView) convertView
							.findViewById(R.id.iv_2);
					holder.iv_3 = (ImageView) convertView
							.findViewById(R.id.iv_3);
					holder.iv_4 = (ImageView) convertView
							.findViewById(R.id.iv_4);
				} catch (Exception e) {
					// TODO: handle exception
				}
			}// 版型3
			else if (getItemViewType(position) == 2) {
				convertView = inflater.inflate(
						R.layout.mypoly_adapter_import_layout2, null);
				// 相册名
				holder.photo_name = (TextView) convertView
						.findViewById(R.id.photo_name);
				// img_count数量
				holder.img_count = (TextView) convertView
						.findViewById(R.id.img_count);
				holder.lock = (ImageView) convertView.findViewById(R.id.lock);

				try {
					// 大图
					holder.iv_0 = (ImageView) convertView
							.findViewById(R.id.iv_0);
					holder.iv_1 = (ImageView) convertView
							.findViewById(R.id.iv_1);
					holder.iv_2 = (ImageView) convertView
							.findViewById(R.id.iv_2);
					holder.iv_3 = (ImageView) convertView
							.findViewById(R.id.iv_3);
				} catch (Exception e) {
					// TODO: handle exception
				}
			}

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		// 版型1
		if (getItemViewType(position) == 0) {
			initBanxing1(position, holder);
		} else if (getItemViewType(position) == 1) {
			initBanxing2(position, holder);
		} else if (getItemViewType(position) == 2) {
			initBanxing3(position, holder);
		}
		return convertView;
	}

	/**
	 * 1号版型 
	 * 
	 * @param position
	 * @param holder
	 */
	private void initBanxing1(int position, ViewHolder holder) {
		// TODO Auto-generated method stub
		if (mList.get(position).getStatus().equals("1")) {
			// 相册名
			holder.photo_name.setText("" + mList.get(position).getPhoto_name());
			// img_count数量
			holder.img_count.setText("(" + mList.get(position).getImg_count()
					+ ")");
			holder.lock.setVisibility(View.GONE);
			
			holder.iv_0.setVisibility(View.INVISIBLE);
			holder.iv_1.setVisibility(View.INVISIBLE);
			holder.iv_2.setVisibility(View.INVISIBLE);
			holder.iv_3.setVisibility(View.INVISIBLE);
			holder.iv_4.setVisibility(View.INVISIBLE);
			
			if(mList.get(position).getShow_img()!=null)
			{
				if(mList.get(position).getShow_img().size()>0)
				{
					holder.iv_0.setVisibility(View.VISIBLE);
					try {
						// 大图
						ImageLoader.getInstance().displayImage(
								Constants.BadiDownImgUrl
										+ "/"
										+ mList.get(position).getShow_img()
												.get(0).getPath()
										+ Constants.AppmomentImg, holder.iv_0,
								options);
					} catch (Exception e) {
						// TODO: handle exception
					}
				}
				if(mList.get(position).getShow_img().size()>1)
				{
					holder.iv_1.setVisibility(View.VISIBLE);
					// 小图
					ImageLoader.getInstance().displayImage(
							Constants.BadiDownImgUrl
									+ "/"
									+ mList.get(position).getShow_img()
											.get(1).getPath()
									+ Constants.Appalbum1, holder.iv_1,
							options);
				}
				if(mList.get(position).getShow_img().size()>2)
				{
					holder.iv_2.setVisibility(View.VISIBLE);
					// 小图
					ImageLoader.getInstance().displayImage(
							Constants.BadiDownImgUrl
									+ "/"
									+ mList.get(position).getShow_img()
											.get(2).getPath()
									+ Constants.Appalbum1, holder.iv_2,
							options);
				}
				if(mList.get(position).getShow_img().size()>3)
				{
					holder.iv_3.setVisibility(View.VISIBLE);
					// 小图
					ImageLoader.getInstance().displayImage(
							Constants.BadiDownImgUrl
									+ "/"
									+ mList.get(position).getShow_img()
											.get(3).getPath()
									+ Constants.Appalbum1, holder.iv_3,
							options);
				}
				if(mList.get(position).getShow_img().size()>4)
				{
					holder.iv_4.setVisibility(View.VISIBLE);
					// 小图
					ImageLoader.getInstance().displayImage(
							Constants.BadiDownImgUrl
									+ "/"
									+ mList.get(position).getShow_img()
											.get(4).getPath()
									+ Constants.Appalbum1, holder.iv_4,
							options);
				}
			}


		}
	}
	/**
	 * 2号版型
	 * 
	 * @param position
	 * @param holder
	 */
	private void initBanxing2(int position, ViewHolder holder) {
		
		if (mList.get(position).getStatus().equals("1")) {
			// 相册名
			holder.photo_name.setText("" + mList.get(position).getPhoto_name());
			// img_count数量
			holder.img_count.setText("(" + mList.get(position).getImg_count()
					+ ")");
			holder.lock.setVisibility(View.GONE);
			
			holder.iv_0.setVisibility(View.INVISIBLE);
			holder.iv_1.setVisibility(View.INVISIBLE);
			holder.iv_2.setVisibility(View.INVISIBLE);
			holder.iv_3.setVisibility(View.INVISIBLE);
			holder.iv_4.setVisibility(View.INVISIBLE);
			
			if(mList.get(position).getShow_img()!=null)
			{
				if(mList.get(position).getShow_img().size()>0)
				{
					holder.iv_0.setVisibility(View.VISIBLE);
					try {
						// 大图
						ImageLoader.getInstance().displayImage(
								Constants.BadiDownImgUrl
										+ "/"
										+ mList.get(position).getShow_img()
												.get(0).getPath()
										+ Constants.Album2big, holder.iv_0,
								options);
					} catch (Exception e) {
						// TODO: handle exception
					}
				}
				if(mList.get(position).getShow_img().size()>1)
				{
					holder.iv_1.setVisibility(View.VISIBLE);
					// 大图
					ImageLoader.getInstance().displayImage(
							Constants.BadiDownImgUrl
									+ "/"
									+ mList.get(position).getShow_img()
											.get(1).getPath()
									+ Constants.App9block, holder.iv_1,
							options);
				}
				if(mList.get(position).getShow_img().size()>2)
				{
					holder.iv_2.setVisibility(View.VISIBLE);
					// 大图
					ImageLoader.getInstance().displayImage(
							Constants.BadiDownImgUrl
									+ "/"
									+ mList.get(position).getShow_img()
											.get(2).getPath()
									+ Constants.App9block, holder.iv_2,
							options);
				}
				if(mList.get(position).getShow_img().size()>3)
				{
					holder.iv_3.setVisibility(View.VISIBLE);
					// 大图
					ImageLoader.getInstance().displayImage(
							Constants.BadiDownImgUrl
									+ "/"
									+ mList.get(position).getShow_img()
											.get(3).getPath()
									+ Constants.App9block, holder.iv_3,
							options);
				}
				if(mList.get(position).getShow_img().size()>4)
				{
					holder.iv_4.setVisibility(View.VISIBLE);
					// 大图
					ImageLoader.getInstance().displayImage(
							Constants.BadiDownImgUrl
									+ "/"
									+ mList.get(position).getShow_img()
											.get(4).getPath()
									+ Constants.App9block, holder.iv_4,
							options);
				}
			}


		}
	}
	private void initBanxing3(int position, ViewHolder holder) {
		if (mList.get(position).getStatus().equals("1")) {
			// 相册名
			holder.photo_name.setText("" + mList.get(position).getPhoto_name());
			// img_count数量
			holder.img_count.setText("(" + mList.get(position).getImg_count()
					+ ")");
			holder.lock.setVisibility(View.GONE);
			
			holder.iv_0.setVisibility(View.INVISIBLE);
			holder.iv_1.setVisibility(View.INVISIBLE);
			holder.iv_2.setVisibility(View.INVISIBLE);
			holder.iv_3.setVisibility(View.INVISIBLE);
			if(mList.get(position).getShow_img()!=null)
			{
				if(mList.get(position).getShow_img().size()>0)
				{
					holder.iv_0.setVisibility(View.VISIBLE);
					try {
						// 大图
						ImageLoader.getInstance().displayImage(
								Constants.BadiDownImgUrl
										+ "/"
										+ mList.get(position).getShow_img()
												.get(0).getPath()
										+ Constants.Appalbum3, holder.iv_0,
								options);
					} catch (Exception e) {
						// TODO: handle exception
					}
				}
				if(mList.get(position).getShow_img().size()>1)
				{
					holder.iv_1.setVisibility(View.VISIBLE);
					// 大图
					ImageLoader.getInstance().displayImage(
							Constants.BadiDownImgUrl
									+ "/"
									+ mList.get(position).getShow_img()
											.get(1).getPath()
									+ Constants.Appalbum3, holder.iv_1,
							options);
				}
				if(mList.get(position).getShow_img().size()>2)
				{
					holder.iv_2.setVisibility(View.VISIBLE);
					// 大图
					ImageLoader.getInstance().displayImage(
							Constants.BadiDownImgUrl
									+ "/"
									+ mList.get(position).getShow_img()
											.get(2).getPath()
									+ Constants.Appalbum3, holder.iv_2,
							options);
				}
				if(mList.get(position).getShow_img().size()>3)
				{
					holder.iv_3.setVisibility(View.VISIBLE);
					// 大图
					ImageLoader.getInstance().displayImage(
							Constants.BadiDownImgUrl
									+ "/"
									+ mList.get(position).getShow_img()
											.get(3).getPath()
									+ Constants.Appalbum3, holder.iv_3,
							options);
				}
			}
			
//			if (mList.get(position).getShow_img() != null) {
//
//				// 大图
//				if (mList.get(position).getShow_img().size() < 1) {
//					holder.iv_0.setImageResource(R.drawable.noimage);
//				} else {
//					try {
//						// 大图
//						ImageLoader.getInstance().displayImage(
//								Constants.BadiDownImgUrl
//										+ "/"
//										+ mList.get(position).getShow_img()
//												.get(0).getPath()
//										+ Constants.App9block, holder.iv_0,
//								options);
//					} catch (Exception e) {
//						// TODO: handle exception
//					}
//				}
//				// 第一张小图
//				if (mList.get(position).getShow_img().size() < 2)
//
//				{
//					holder.iv_1.setImageResource(R.drawable.noimage);
//				}
//
//				else {
//
//					try {
//						ImageLoader.getInstance().displayImage(
//								Constants.BadiDownImgUrl
//										+ "/"
//										+ mList.get(position).getShow_img()
//												.get(1).getPath()
//										+ Constants.appPhoto4img, holder.iv_1,
//								options);
//					} catch (Exception e) {
//						// TODO: handle exception
//					}
//				}
//				// 第二张小图
//				if (mList.get(position).getShow_img().size() < 3) {
//					holder.iv_2.setImageResource(R.drawable.noimage);
//				} else {
//
//					try {
//						ImageLoader.getInstance().displayImage(
//								Constants.BadiDownImgUrl
//										+ "/"
//										+ mList.get(position).getShow_img()
//												.get(2).getPath()
//										+ Constants.appPhoto4img, holder.iv_2,
//								options);
//					} catch (Exception e) {
//						// TODO: handle exception
//					}
//				}
//				// 第三张小图
//				if (mList.get(position).getShow_img().size() < 4) {
//					holder.iv_3.setImageResource(R.drawable.noimage);
//				} else {
//
//					try {
//						ImageLoader.getInstance().displayImage(
//								Constants.BadiDownImgUrl
//										+ "/"
//										+ mList.get(position).getShow_img()
//												.get(3).getPath()
//										+ Constants.appPhoto4img, holder.iv_3,
//								options);
//					} catch (Exception e) {
//						// TODO: handle exception
//					}
//				}
//
//			} else {
//				holder.iv_0.setImageResource(R.drawable.noimage);
//				holder.iv_1.setImageResource(R.drawable.noimage);
//				holder.iv_2.setImageResource(R.drawable.noimage);
//				holder.iv_3.setImageResource(R.drawable.noimage);
//
//			}

		}

	}

	static class ViewHolder {
		// 相册名
		public TextView photo_name;

		// img_count数量
		public TextView img_count;

		ImageView iv_0;

		ImageView iv_1;

		ImageView iv_2;

		ImageView iv_3;

		ImageView iv_4;
		public ImageView lock;

	}

	public String getVersion() {
		return Version;
	}

//	public void setDownPicWH(final ImageView iv, final int newWidth,
//			final int newHeight) {
//		ImageLoader.getInstance().loadImage("", options,
//				new ImageLoadingListener() {
//
//					@Override
//					public void onLoadingStarted(String arg0, View arg1) {
//						// TODO Auto-generated method stub
//
//					}
//
//					@Override
//					public void onLoadingFailed(String arg0, View arg1,
//							FailReason arg2) {
//						// TODO Auto-generated method stub
//
//					}
//
//					@Override
//					public void onLoadingComplete(String arg0, View arg1,
//							Bitmap arg2) {
//						// TODO Auto-generated method stub
//						iv.setImageBitmap(Utils.scaleImg(arg2, newWidth,
//								newHeight));
//					}
//
//					@Override
//					public void onLoadingCancelled(String arg0, View arg1) {
//						// TODO Auto-generated method stub
//
//					}
//				});
//	};

	public void setVersion(String version) {
		Version = version;
	}
}
